<?php

/**
 * uniBoard ACP Edit Help File
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_help{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Available Parents
	 *
	 * @var array
	 */
	
	private $parents		= array();
	
	/**
	 * Allow Parents
	 *
	 * @var bool
	 */
	
	private $allow_parent	= true;
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_help_file( $appCore, $appCore -> _REQ( 'file'));
		
		// Api Loaded?
		if ( $this -> api -> data_loaded)
		{
			// Load fields templates
			$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
			
			// Edit or delete?
			if ( $appCore -> _REQ( 'do') == 'edit')
			{	
				// Set file first parent
				$this -> parents[0] = $appCore -> lang['acp_help_file_parent_0'];
				
				// Select Files
				$appCore -> db -> query( 'SELECT file_id, file_parent, file_name
				FROM ' . DB_PREFIX . 'help_files
				WHERE file_parent IN (0,' . $this -> api -> file_id . ')
				ORDER BY file_pos ASC');
				
				// Parse result
				while ( $file_result = $appCore -> db -> fetch_array())
				{
					if ( $file_result[1] == 0)
					{
						// Add To Parents
						$this -> parents[$file_result[0]] = $appCore -> ifString( htmlspecialchars( $file_result[2]));
					}
					else
					{
						// This means we have subfiles, disallow parents
						$this -> allow_parent = false;
					}
				}
				
				// Free result
				$appCore -> db -> freeResult();
					
				// Remove our style from parents?
				if ( isset( $this -> parents[$this -> api -> file_id]))
				{
					unset( $this -> parents[$this -> api -> file_id]);
				}
			
				// Update?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Fill Api
					$this -> api -> setFileName( $appCore -> _POST( 'file_name'));
					$this -> api -> setFileText( $appCore -> _POST( 'file_text'));
					
					// Set parent?
					if ( $this -> allow_parent)
					{
						$this -> api -> setFileParent( $appCore -> _POST( 'file_parent'));
						
						// Parent incorrect?
						if ( !isset( $this -> parents[$appCore -> _POST( 'file_parent')]))
						{
							$this -> api -> setError( $appCore -> lang['acp_help_file_parent_not_found']);
						}
					}
					
					// Api All-rite?
					if ( $this -> api -> error)
					{
						// Draw Errors
						$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> ifString( htmlspecialchars( $this -> api -> file_name)));
						
						// Repeat Form
						$this -> drawForm();
					}
					else
					{
						// Save Api
						$this -> api -> saveData();
						
						// Set Log
						$appCore -> logAdmin( 'log_admin_edited_help_file', htmlspecialchars( $appCore -> _POST( 'file_name')));
						
						// Draw Message
						$appCore -> output -> message( $appCore -> lang['acp_help_file_edited'], $appCore -> ifString( htmlspecialchars( $this -> api -> file_name)));
						
						// Jump back to files list
						new action_help_system( $appCore);
					}
				}
				else
				{
					// Draw Form
					$this -> drawForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'delete')
			{
				// Set Replaces
				$this -> parents[-1] = $appCore -> lang['acp_help_file_replace_-1'];
				$this -> parents[0] = $appCore -> lang['acp_help_file_replace_0'];
				
				// Select Files
				$appCore -> db -> query( 'SELECT file_id, file_name
				FROM ' . DB_PREFIX . 'help_files
				WHERE file_parent = \'0\'
				ORDER BY file_pos ASC');
				
				// Parse result
				while ( $file_result = $appCore -> db -> fetch_array())
				{
					$this -> parents[$file_result[0]] = $appCore -> ifString( htmlspecialchars( $file_result[1]));
				}
				
				// Free result
				$appCore -> db -> freeResult();
		
				// Delete our file from replaces?
				if ( isset( $this -> parents[$this -> api -> file_id]))
				{
					unset( $this -> parents[$this -> api -> file_id]);
				}
				
				// Update?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Get Replacement
					$file_replace = (integer) $appCore -> _POST( 'file_replace');
					
					// Is it allrite?
					if ( isset( $this -> parents[$file_replace]))
					{
						// Kill cache
						class_cache::flushCache( 'help_files');
												
						// Dont replace
						if ( $file_replace == -1)
						{
							// Delete all files
							$appCore -> db -> delete( 'help_files', 'file_id = \'' . $this -> api -> file_id . '\' OR file_parent = \'' . $this -> api -> file_id . '\''); 
						
							// Set Log
							$appCore -> logAdmin( 'log_admin_deleted_help_file_and_children', htmlspecialchars( $this -> api -> file_name));
							
							// Redirect us back to list
							$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=help_system'), $appCore -> lang['acp_help_file_deleted']);	
						}
						else
						{
							// Delete our file
							$appCore -> db -> delete( 'help_files', 'file_id = \'' . $this -> api -> file_id . '\''); 
						
							// Replace
							$appCore -> db -> update( array( 'file_parent' => $file_replace), 'help_files', 'file_parent = \'' . $this -> api -> file_id . '\''); 
							
							// Set Log
							$appCore -> logAdmin( 'log_admin_deleted_help_file', htmlspecialchars( $this -> api -> file_name));
							
							// Redirect us back to list
							$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=help_system'), $appCore -> lang['acp_help_file_deleted_and_moved']);	
						}
					}
					else
					{
						// Draw Error
						$appCore -> output -> error( $appCore -> lang['acp_help_file_not_found'], $appCore -> ifString( htmlspecialchars( $this -> api -> file_name)));
						
						// Repeat Form
						$this -> drawDeleteForm();
					}
				}
				else
				{
					// Draw form
					$this -> drawDeleteForm();
				}
			}
			else
			{
				// Draw Error
				$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_help_system']);
						
				// Jump back to files list
				new action_help_system( $appCore);
			}
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_help_file_not_found'], $appCore -> lang['acp_help_system']);
					
			// Jump back to files list
			new action_help_system( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
			
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_help&do=edit&file=' . $this -> api -> file_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Allow File Parent?
		if ( $this -> allow_parent)
		{
			// Draw field
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_help_file_parent'], $this-> appCore -> drawList( 'file_parent', $this -> parents, $this -> api -> file_parent));
		}
		
		// File Name
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_help_file_name'], $this-> appCore -> drawTextInput( 'file_name', $this -> api -> file_name));
		
		// Draw Text Field
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_help_file_text']);
		
		// Draw Row
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'file_text', $this -> api -> file_text));
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['edit_help_file_form'], array(
			$this -> appCore -> adminLink( 'act=help_system') => $this -> appCore -> lang['acp_help_system'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> file_name))
		), ob_get_clean());
			
	}

	// ------------------------------------------------------------------------

	/**
	 * Draws Deletion Form
	 *
	 */
	
	function drawDeleteForm(){
			
		// Cache output
		ob_start();
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_help&do=delete&file=' . $this -> api -> file_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_help_file_replace'], $this-> appCore -> drawList( 'file_replace', $this -> parents, $this -> api -> file_parent), $this -> appCore -> lang['acp_help_file_replace_expl']);
			
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['delete']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
				
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['delete_help_file_form'], array(
			$this -> appCore -> adminLink( 'act=help_system') => $this -> appCore -> lang['acp_help_system'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> file_name))
		), ob_get_clean());
		
	}
	
}